﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//请先添加引用
namespace Maticsoft.DAL.Jobs
{
    /// <summary>
    /// 数据访问类Job。
    /// </summary>
    public class Job
    {
        public Job()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Job");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Maticsoft.Model.Jobs.Job model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Job(");
            strSql.Append("classId,jobName,count,sex,education,address,[desc],requirements,remarks,issue,deployDate,email,penone,deptId)");
            strSql.Append(" values (");
            strSql.Append("@classId,@jobName,@count,@sex,@education,@address,@desc,@requirements,@remarks,@issue,@deployDate,@email,@penone,@deptId)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@classId", SqlDbType.Int,4),
					new SqlParameter("@jobName", SqlDbType.NVarChar,255),
					new SqlParameter("@count", SqlDbType.Int,4),
					new SqlParameter("@sex", SqlDbType.Char,2),
					new SqlParameter("@education", SqlDbType.NVarChar,50),
					new SqlParameter("@address", SqlDbType.NVarChar,50),
					new SqlParameter("@desc", SqlDbType.NVarChar,255),
					new SqlParameter("@requirements", SqlDbType.NChar,255),
					new SqlParameter("@remarks", SqlDbType.NVarChar,255),
					new SqlParameter("@issue", SqlDbType.NVarChar,10),
					new SqlParameter("@deployDate", SqlDbType.DateTime),
					new SqlParameter("@email", SqlDbType.NVarChar,50),
					new SqlParameter("@penone", SqlDbType.NVarChar,50),
					new SqlParameter("@deptId", SqlDbType.Int,4)};
            parameters[0].Value = model.classId;
            parameters[1].Value = model.jobName;
            parameters[2].Value = model.count;
            parameters[3].Value = model.sex;
            parameters[4].Value = model.education;
            parameters[5].Value = model.address;
            parameters[6].Value = model.desc;
            parameters[7].Value = model.requirements;
            parameters[8].Value = model.remarks;
            parameters[9].Value = model.issue;
            parameters[10].Value = model.deployDate;
            parameters[11].Value = model.email;
            parameters[12].Value = model.penone;
            parameters[13].Value = model.deptId;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Maticsoft.Model.Jobs.Job model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Job set ");
            strSql.Append("classId=@classId,");
            strSql.Append("jobName=@jobName,");
            strSql.Append("count=@count,");
            strSql.Append("sex=@sex,");
            strSql.Append("education=@education,");
            strSql.Append("address=@address,");
            strSql.Append("[desc]=@desc,");
            strSql.Append("requirements=@requirements,");
            strSql.Append("remarks=@remarks,");
            strSql.Append("issue=@issue,");
            strSql.Append("deployDate=@deployDate,");
            strSql.Append("email=@email,");
            strSql.Append("penone=@penone,");
            strSql.Append("deptId=@deptId");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@classId", SqlDbType.Int,4),
					new SqlParameter("@jobName", SqlDbType.NVarChar,255),
					new SqlParameter("@count", SqlDbType.Int,4),
					new SqlParameter("@sex", SqlDbType.Char,2),
					new SqlParameter("@education", SqlDbType.NVarChar,50),
					new SqlParameter("@address", SqlDbType.NVarChar,50),
					new SqlParameter("@desc", SqlDbType.NVarChar,255),
					new SqlParameter("@requirements", SqlDbType.NChar,255),
					new SqlParameter("@remarks", SqlDbType.NVarChar,255),
					new SqlParameter("@issue", SqlDbType.NVarChar,10),
					new SqlParameter("@deployDate", SqlDbType.DateTime),
					new SqlParameter("@email", SqlDbType.NVarChar,50),
					new SqlParameter("@penone", SqlDbType.NVarChar,50),
					new SqlParameter("@deptId", SqlDbType.Int,4)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.classId;
            parameters[2].Value = model.jobName;
            parameters[3].Value = model.count;
            parameters[4].Value = model.sex;
            parameters[5].Value = model.education;
            parameters[6].Value = model.address;
            parameters[7].Value = model.desc;
            parameters[8].Value = model.requirements;
            parameters[9].Value = model.remarks;
            parameters[10].Value = model.issue;
            parameters[11].Value = model.deployDate;
            parameters[12].Value = model.email;
            parameters[13].Value = model.penone;
            parameters[14].Value = model.deptId;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Job ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Maticsoft.Model.Jobs.Job GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,classId,jobName,count,sex,education,address,[desc],requirements,remarks,issue,deployDate,email,penone,deptId from Job ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            Maticsoft.Model.Jobs.Job model = new Maticsoft.Model.Jobs.Job();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["classId"].ToString() != "")
                {
                    model.classId = int.Parse(ds.Tables[0].Rows[0]["classId"].ToString());
                }
                model.jobName = ds.Tables[0].Rows[0]["jobName"].ToString();
                if (ds.Tables[0].Rows[0]["count"].ToString() != "")
                {
                    model.count = int.Parse(ds.Tables[0].Rows[0]["count"].ToString());
                }
                model.sex = ds.Tables[0].Rows[0]["sex"].ToString();
                model.education = ds.Tables[0].Rows[0]["education"].ToString();
                model.address = ds.Tables[0].Rows[0]["address"].ToString();
                model.desc = ds.Tables[0].Rows[0]["desc"].ToString();
                model.requirements = ds.Tables[0].Rows[0]["requirements"].ToString();
                model.remarks = ds.Tables[0].Rows[0]["remarks"].ToString();
                model.issue = ds.Tables[0].Rows[0]["issue"].ToString();
                if (ds.Tables[0].Rows[0]["deployDate"].ToString() != "")
                {
                    model.deployDate = DateTime.Parse(ds.Tables[0].Rows[0]["deployDate"].ToString());
                }
                model.email = ds.Tables[0].Rows[0]["email"].ToString();
                model.penone = ds.Tables[0].Rows[0]["penone"].ToString();
                if (ds.Tables[0].Rows[0]["deptId"].ToString() != "")
                {
                    model.deptId = int.Parse(ds.Tables[0].Rows[0]["deptId"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,classId,jobName,count,sex,education,address,[desc],requirements,remarks,issue,deployDate,email,penone,deptId ");
            strSql.Append(" FROM Job ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,classId,jobName,count,sex,education,address,[desc],requirements,remarks,deployDate");
          //  strSql.Append(" id,classId,jobName,count,sex,education,address,[desc],requirements,remarks,issue,deployDate,email,penone,deptId ");
            strSql.Append(" FROM Job ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Job";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
         /// <summary>
        /// 删除
        /// </summary>
        /// <param name="strWhere"></param>
        public void DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete job ");           
            if (Idlist.Trim() != "")
            {
                strSql.Append(" where  id in (" + Idlist + ")");
            }
            DbHelperSQL.ExecuteSql(strSql.ToString());

        }
        /// <summary>
        /// 发布
        /// </summary>
        /// <param name="strWhere"></param>
        public void ReleaseList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update job set issue='true'");
            if (Idlist.Trim() != "")
            {
                strSql.Append(" where  id in (" + Idlist + ")");
            }
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 休眠(取消发布)
        /// </summary>
        /// <param name="strWhere"></param>
        public void NoReleaseList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update job set issue='false'");
            if (Idlist.Trim() != "")
            {
                strSql.Append( " where  id in (" + Idlist + ")");
            }
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }
        public DataSet GetTopScroll(int top)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" issue='true'");
            return GetList(top, strSql.ToString(), "deployDate desc");
        }
        public DataSet GetTopScroll(int top, string deptId, string classId)
        {
            
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" issue='true'");
            if (!deptId.Equals(""))
            {
                strSql.Append(" and deptId=");
                strSql.Append(deptId);
            }
            if (!classId.Equals(""))
            {
                strSql.Append(" and classId=");
                strSql.Append(classId);
            }
            return GetList(top, strSql.ToString(), "deployDate desc");
        }
    
    }
}

